package com.mall.controller;


import cn.hutool.core.bean.BeanUtil;
import com.mall.constant.StatusConstant;
import com.mall.dto.BrandDTO;
import com.mall.dto.BrandPageQueryDTO;
import com.mall.po.Brand;
import com.mall.result.PageResult;
import com.mall.result.Result;
import com.mall.service.BrandService;
import com.mall.vo.BrandVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author itlxc
 * @since 2024-04-18
 */
@RestController
@RequestMapping("/admin/brand")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "品牌相关接口")
public class BrandController {

    private final BrandService brandService;

    @PostMapping("/add")
    @ApiOperation("添加品牌")
    public Result saveBrand(@RequestBody BrandDTO brandDTO) {
        log.info("添加品牌：{}", brandDTO);
        Brand brand = BeanUtil.copyProperties(brandDTO, Brand.class);
        brand.setStatus(StatusConstant.DISABLE);
        brandService.save(brand);
        return Result.success();
    }

    /**
     * 品牌分页查询
     * @param brandPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("品牌分页查询")
    public Result<PageResult> brandPageQuery(BrandPageQueryDTO brandPageQueryDTO) {
        log.info("品牌分页查询：{}", brandPageQueryDTO);
        PageResult pageResult = brandService.brandPageQuery(brandPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询品牌
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询品牌")
    public Result<BrandVO> getBrandById(@PathVariable Long id) {
        log.info("根据id查询品牌：{}", id);
        Brand brand = brandService.getById(id);
        BrandVO brandVo = BeanUtil.copyProperties(brand, BrandVO.class);
        return Result.success(brandVo);
    }

    /**
     * 修改品牌
     * @param brandDTO
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改品牌")
    public Result updateBrand(@RequestBody BrandDTO brandDTO) {
        log.info("修改品牌：{}", brandDTO);
        Brand brand = BeanUtil.copyProperties(brandDTO, Brand.class);
        brandService.updateById(brand);
        return Result.success();
    }

    /**
     * 删除品牌
     * @param ids
     * @return
     */
    @DeleteMapping("/del")
    @ApiOperation("删除品牌")
    public Result deleteBrand(@RequestParam List<Long> ids) {
        log.info("删除品牌：{}", ids);
        brandService.deleteBrand(ids);
        return Result.success();
    }

    /**
     * 查询启用状态的品牌
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询启用状态的品牌")
    public Result<List<Brand>> getBrandByStatus() {
        log.info("查询启用状态的品牌");
        List<Brand> brandList = brandService.lambdaQuery().eq(Brand::getStatus, StatusConstant.ENABLE).list();
        return Result.success(brandList);
    }

    /**
     * 启用、禁用品牌
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用、禁用品牌")
    public Result startOrStopBrand(@PathVariable Integer status, Long id) {
        log.info("启用、禁用品牌: id: {}, status: {}", id, status == 1 ? "启用" : "禁用");
        brandService.startOrStopBrand(status, id);
        return Result.success();
    }
}
